<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="/js/jquery-3.4.1.min.js"></script>
</head>
<body>
<form id="user-insert">
    <input type="text" name="username">账号<br>
    <input type="text" name="password">密码<br>
    <select id="org-list" name="organization.id">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <select id="role-list" name="roles">
        <option value="1">2</option>
        <option value="3">4</option>
        <option value="5">6</option>
    </select><br>
    <input type="button" onclick="userInsert()">提交
</form>


<script>
    $(document).ready(function () {
        getOrgObj();
        getRoleList();
    })

    function getOrgObj() {
        $("#org-list").empty();
        $.ajax({
            "dataType": "json",
            "type": "get",
            "url": "/organizetion/selectIdOrName",
            "success": function (result) {
                for (var i = 0; i < result.data.length; i++) {
                    var html = '<option value="#{orgId}">#{orgName}</option>'
                    html = html.replace("#{orgId}",result.data[i].id)
                    html = html.replace("#{orgName}", result.data[i].org_name)
                    $("#org-list").append(html)
                }
            }
        })
    }

    function getRoleList() {
        $("#role-list").empty();
        $.ajax({
            "dataType": "json",
            "type": "get",
            "url": "/role/selectAll",
            "success": function (result) {
                const stringList=new Array();
                for (var i = 0; i < result.data.length; i++) {
                    stringList.push(result.data[i].id)
                    var html = '<option value="#{roleId}" >#{roleName}</option>'
                    html = html.replace("#{roleId}",stringList)
                    html = html.replace("#{roleName}", result.data[i].name)
                    $("#role-list").append(html)
                }
            }
        })
    }

    function userInsert() {
        $.ajax({
            "dataType": "json",
            "type": "put",
            "url": "/user/insert",
            "data": $("#user-insert").serialize(),
            traditional: true,
            "success": function (result) {
                alert("添加成功")
            },
            "error": function (err) {
                alert("添加失败" + err.message);
            }
        })
    }
</script>
</body>
</html>